<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Session;
use think\Config;
use app\common\util\WechatThirdUtil;
use app\common\model\AuthorWxUser as AuthorWxUserModel;
use app\common\model\WxUser as WxUserModel;

class Custommenu extends AdminBase
{
    protected $config;
    protected $third_config;
    protected $wx_user_model;
    protected $author_wx_user_model;

    protected function _initialize(){
        parent::_initialize();
        $this->wx_user_model = new WxUserModel();
        $this->config = Session::get('wechat_info'); //当前授权公众号信息
        $this->third_config = $this->wx_user_model->find(); //第三方开发平台信息
        $this->wechat_obj = new WechatThirdUtil($this->third_config , $this->config);
        $this->author_wx_user_model = new AuthorWxUserModel();
    }

    public function index(){
        return $this->fetch();
    }

        /*
     * 需要获取当前公众号的菜单栏数据
     * http请求方式：GET
     * */
    public function menunow(){
        // 从公众号获取菜单栏json  getAccessToken
        $wechat_obj = $this->wechat_obj;
        $wx_menu_json = $wechat_obj->getMenu();
        return  $this->thinkJson($wx_menu_json);
//        return $this->fetch();
    }

    public function menu(){
       return $this->fetch();
    }


    public function saveMenu(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $wechat_obj = $this->wechat_obj;
            $result = $wechat_obj->createMenu($data['button']);
            // if($result['errcode'] == 0){
            //     $menu_data = ['menu_config' => json($data)];
            //     $menu_result = $this->author_wx_user_model->save($menu_data,['id' => $this->config['id']]);
            //     if($menu_result){
            //         return json(['status' => 1 , 'msg' => '菜单保存成功']);
            //     }else{
            //         return json(['status' => -1 , 'error' => '菜单保存失败']);
            //     }
            // }else{
            //     return json(['status' => -1 , 'error' => '菜单创建失败']);
            // }
        }
    }

    /*
     * 新增模板消息测试接口
     * 获取设置的行业信息
     * */
    public function getTemplateList(){
        // 1 获取模板列表
        $template_list = $this->wechat_obj->getTemplateList();
        return $this->thinkJson($template_list);
    }

    
}

